from Systems.Solar import *

import random
import math

EARTHMASS = 5.9742*10**24 # *10**24 kg 
EARTHVOLUME = 1.0832073*10**12 #1.0832073x10**12 km3
EARTHDENSITY = 5.5153 # g/cm3
PLANETARYBODY = 6*10**24

class Planet(SolarBody):
          
    atmosphereRich = None
    atmosphereTrace = None
    atmosphereAbundance = None
        
    resourceNodes = []
    
    moons = []
    
    def __str__(self):
       s = "[Name:" + self.getName()
       s = s + " Type:" + self.type
       s = s + " Class:" + self.letterCode
       s = s + " Mass:"  + str(self.mass) + "x10^22"
       s = s + " Orbital Radius:" + str(self.orbit) + " AU"
       return s  + " ]"

class PlanetAtmosphereEnum():
    NONE = "No Atmosphere"
    INERT = "Helium"
    NITROGEN = "Nitrogen"
    OXYGEN = "Oxygen"
    CARBON = "Carbon Dioxide"
    SULFUR = "Sulfur Dioxide"
    HYDROGEN = "Hydrogen"
    METHANE = "Methane"
    AMMONIA = "Ammonia"
    
class PlanetFactory():
    
    # Masses in Earths
    massLower = 0
    massUpper = 0
    
    # Density in g/cm3
    densityLower = 0
    densityUpper = 0
            
    def makePlanet(self): abstract 
    
    def randomPlanet(self, type, letter):
        p = Planet()
        p.type = type
        p.letterCode = letter
        
        p.mass = self.randomMass()
        p.radius = self.calculateRadius(p.mass, self.radomDensity())
        
        p.atmosphere = self.randomAtomsphere()
        p.resourceNodes = self.randomResources()
        
        return p
            
    def randomAtomsphere(self):
        return PlanetAtmosphereEnum.NONE
        
    def randomMass(self):
        m = (random.random() * (self.massUpper - self.massLower)) + self.massLower
        return (m * EARTHMASS)
    
    def radomDensity(self):
        return ((random.random() * (self.densityUpper - self.densityLower)) + self.densityLower)    
 
    def calculateVolume(self, mass, density):
        d = (density * 1000 ) # conver to kg/m3
        return (mass * 10**22) / d # d = m/v, mass is in kg 10**22, v is in m3
    
    def calculateRadius(self, mass, density):  
        d = (density * 1000 ) # conver to kg/m3
        v = (mass * 10**22) / d # d = m/v, mass is in kg 10**22, v is in m3
        return self.convertVolumeToRadius(v)
    
    def randomResources(self):
        return []
    
    def convertVolumeToRadius(self, vol):   
        # ((V * 3/4) / PI] )^(1/3)
        r = ((vol * 3/4) / math.pi)**(1/3)    
        return (r / 1000)